package day03;

import lombok.val;

import java.util.HashMap;
import java.util.Map;

public class MapPractice {
    public static void main(String[] args) {
        Map<String,String> map = new HashMap<>();
        map.put("a","A");
        map.put("b","B");
        map.put("c","C");
        map.put("d","D");
        map.put("e","A");
        map.put("f","B");
        map.put("g","C");
        map.put("h","D");
        map.put("i","A");
        map.put("j","B");
        map.put("k","C");
        map.put("l","D");
        map.put("m","A");
        map.put("n","B");
        map.put("o","A");
        //统计投票数
        counter(map);
    }
    public static void counter(Map<String,String> map){
        //1.创建新的map集合，存储统计结果
        Map<String,Integer> reslutMap = new HashMap<>();
        //2.遍历map集合(key--value)
        map.forEach((k,v)->{
            boolean containsKey = reslutMap.containsKey(v);
            //3.判断v是否已经在reslutMap中存在了
            if(containsKey){
                //4.果A已经存在，则A对应的值加一
                reslutMap.put(v,reslutMap.get(v)+1);
            }else {//5.如果A不存在,直接将A存入新集合中
                reslutMap.put(v,1);
            }
        });
        //6.遍历集合
        reslutMap.forEach((k,v)->{
            System.out.println(k+"---"+v);
        });
    }
}
